package pers.li.dao;

import pers.li.model.Order;
import pers.li.model.TO.OrderGoodTO;
import pers.li.model.TO.OrderTO;
import pers.li.utils.DataProcess;
import pers.li.utils.OrderUtils;
import pers.li.model.OrderDetail;

import java.util.Date;
import java.util.List;

public class OrderDao {
    private final DataProcess dp = new DataProcess(true);

    public int insertOrder(OrderTO orderTO, OrderGoodTO orderGoodTO) {
        String sql = "insert into t_order (order_code, goods_id, user_id, addr_id,order_date)\n" +
                "values (?,?,?,?,?);";
        String orderCode = OrderUtils.getOrderCode();
        return dp.insertRow(sql, orderCode, orderGoodTO.getGoodsId(), orderTO.getUserId(), orderTO.getAddrId(), new Date());
    }

    public int insertOrderDetail(Integer orderId, OrderGoodTO orderGoodTO) {
        String sql = "insert into t_order_detail (order_id, goods_price, order_sales, goods_postalfee, order_price)\n" +
                "values (?,?,?,?,?);";
        return dp.insertRow(sql, orderId, orderGoodTO.getGoodsPrice(),
                orderGoodTO.getOrderSales(), orderGoodTO.getGoodsPostalfee(), orderGoodTO.getOrderPrice());
    }

    public int insertOrderContent(Integer orderId, Integer contentId) {
        String sql = "insert into t_order_content (order_id, content_id)\n" +
                "values (?,?)";
        return dp.insertRow(sql, orderId, contentId);
    }

    public List<Order> getOrderListByUserId(Integer userId) {
        String sql = "select *from t_order where user_id = ?";
        return dp.getObjectList(sql, Order.class, userId);
    }

    public Order getOrderByOrderId(Integer orderId) {
        String sql = "select *from t_order where order_id = ?";
        return dp.getObject(sql, Order.class, orderId);
    }

    public OrderDetail getOrderDetailByOrderId(Integer orderId) {
        String sql = "select *from t_order_detail where order_id = ?";
        return dp.getObject(sql, OrderDetail.class, orderId);
    }

    public List<String> getContentNameByOrderId(Integer orderId) {
        String sql = "select content from t_order_content toc,t_goodsdetail tgd\n" +
                "where toc.content_id = tgd.content_id\n" +
                "AND order_id = ?";
        return dp.getObjectList(sql, String.class, orderId);
    }

    public int updateOrderStatus(Integer orderId, Integer status) {
        String sql = "update t_order\n" +
                "set order_status = ?\n" +
                "where order_id = ?";
        return dp.updateRow(sql, status, orderId);
    }

}
